package com.sp2p.action.front;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.shove.Convert;
import com.shove.util.JSONUtils;
import com.shove.util.SqlInfusion;
import com.sp2p.entity.User;
import com.sp2p.service.UserService;


public class FrontHomePageAction extends BaseFrontAction {

	public static Log log = LogFactory.getLog(FrontHomePageAction.class);
	private static final long serialVersionUID = 1L;
	private UserService userService;

	public String platformInit(){
		return "success";
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String teamInit(){
		return "success";
	}
	
	public String legalPolicyInit(){
		return "success";
	}
	
	public String aboutUsInit(){
		return "success";
	}
	
	public String downloadInit(){
		return "success";
	}
	
	public String queryPzInfo(){
		return "success";
	}
	
	public void getPzInfoList() throws Exception{
		JSONObject obj = new JSONObject();
		User user = this.getUser();
		
		Long userId = user.getId();
		if (userId == null){
			obj.put("msg","noLogin");
			JSONUtils.printObject(obj);
			return;
		}
		
		pageBean.setPageNum(SqlInfusion.FilteSqlInfusion(request("currentPage")));
		pageBean.setPageSize(Convert.strToInt(request("pageSize"), 10));
		
		try {
			userService.queryPzInfoList(userId, pageBean);
		} catch (SQLException e) {
			log.error(e);
			e.printStackTrace();
		}
		
		if( pageBean.getTotalNum() > 0){
			obj.put("voList", JSONUtils.toJSONArray(pageBean.getPage()));
		}else{
			obj.put("voList", "");
		}
		obj.put("currentPage", pageBean.pageNum);
		obj.put("pageCount", pageBean.getTotalPageNum());
		obj.put("recordCount", pageBean.getTotalNum());
		JSONUtils.printObject(obj);
	}
	
	public String matchEndowmentIndex(){
		return "success";
	}
	
	public void addMatchEndowmentApply() throws Exception {
		JSONObject obj = new JSONObject();
		User user = this.getUser();
		
		Long userId = user.getId();
		int isBindIdNo =  user.getIsBindIdNo();
		int isBindPhone = user.getIsBindMobile();
		if (userId == null){
			obj.put("msg","noLogin");
			JSONUtils.printObject(obj);
			return;
		}
		if(isBindIdNo != 1){
			obj.put("msg","bindIdNo");
			JSONUtils.printObject(obj);
			return;
		}
		if(isBindPhone != 1){
			obj.put("msg","bindPhone");
			JSONUtils.printObject(obj);
			return;
		}
		
		int pzCount = 0;
		Map<String, String> pzCountMap = userService.queryPzInfoCount(userId);
		if(pzCountMap != null){
			pzCount = Convert.strToInt(pzCountMap.get("num"), 0) ;
		}
		
		if(pzCount >= 5){
			obj.put("msg","outCounts");
			JSONUtils.printObject(obj);
			return;
		}
		
		Map<String, String> personMap = userService.queryPersonById(user.getId());
		obj.put("realName", user.getRealName());
		obj.put("username", user.getUserName());
		obj.put("idNo", personMap.get("idNo"));
		obj.put("msg","success");
		JSONUtils.printObject(obj);
	}
	
	public void signContract() throws Exception {
		JSONObject obj = new JSONObject();
		Date nowDate = new Date();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		User user = this.getUser();
		
		Long userId = user.getId();
		if (userId == null){
			obj.put("msg","noLogin");
			JSONUtils.printObject(obj);
			return;
		}
		
		int level = Convert.strToInt(paramMap.get("level"), 0);
		Double amount = Convert.strToDouble(paramMap.get("amount"),0);
		Double rate = Convert.strToDouble(paramMap.get("rate"),0);
		int selDeadline = Convert.strToInt(paramMap.get("selDeadline"), 0);
		String applyTime = df.format(nowDate);
		
		int pzCount = 0;
		Map<String, String> pzCountMap = userService.queryPzInfoCount(userId);
		if(pzCountMap != null){
			pzCount = Convert.strToInt(pzCountMap.get("num"), 0) ;
		}
		
		if(pzCount >= 5){
			obj.put("msg","outCounts");
			JSONUtils.printObject(obj);
			return;
		}
		
		Long result = userService.addPzUserApply(level, amount,rate, selDeadline, userId,applyTime);
		
		if(result < 0){
			obj.put("msg","failed");
			JSONUtils.printObject(obj);
			return;
		}
		
		obj.put("msg","success");
		JSONUtils.printObject(obj);
	}
}
